package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.domain.file.FileBusiness;
import com.ruibang.glass.quality.domain.req.EightDReportReq;
import com.ruibang.glass.quality.entity.EightDReport;
import com.ruibang.glass.quality.feign.FileApi;
import com.ruibang.glass.quality.mapper.EightDReportMapper;
import com.ruibang.glass.quality.service.EightDReportService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.seata.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 8D报告 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-05-21
 */
@Slf4j
@Service
public class EightDReportServiceImpl extends ServiceImpl<EightDReportMapper, EightDReport> implements EightDReportService {

    @Autowired
    private FileApi fileApi;

    @Override
    public IPage<EightDReport> pageQuery(EightDReportReq eightDReportReq) {
        QueryWrapper<EightDReport> qw = new QueryWrapper<>();
        qw.lambda().like(StringUtils.isNotBlank(eightDReportReq.getBarcodeCode()), EightDReport::getBarcodeCode, eightDReportReq.getBarcodeCode());
        qw.lambda().like(StringUtils.isNotBlank(eightDReportReq.getContent()), EightDReport::getContent, eightDReportReq.getContent());
        qw.lambda().like(StringUtils.isNotBlank(eightDReportReq.getComplaintUnit()), EightDReport::getComplaintUnit, eightDReportReq.getComplaintUnit());
        qw.lambda().eq(StringUtils.isNotBlank(eightDReportReq.getModel()), EightDReport::getModel, eightDReportReq.getModel());
        if (eightDReportReq.getStartDate() != null && eightDReportReq.getEndDate() != null) {
            qw.lambda().ge(EightDReport::getTime, eightDReportReq.getStartDate());
            qw.lambda().le(EightDReport::getTime, eightDReportReq.getEndDate());
        }
        Page<EightDReport> page = this.page(new Page<>(eightDReportReq.getPageNo(), eightDReportReq.getPageSize()), qw.lambda());
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<EightDReport> eightDReportList = page.getRecords();
            List<String> ids = eightDReportList.stream().map(EightDReport::getId).collect(Collectors.toList());
            ResultBody<List<FileBusiness>> resultBody = fileApi.getFileBusinessByIds(ids);
            if (resultBody != null && CollectionUtils.isNotEmpty(resultBody.getData())) {
                List<FileBusiness> fileData = resultBody.getData();
                Map<String, List<String>> fileMap = fileData.stream().collect(Collectors.toMap(FileBusiness::getBusinessId, FileBusiness::getFileIds));
                for (EightDReport eightDReport : eightDReportList) {
                    eightDReport.setFileIds(fileMap.get(eightDReport.getId()));
                }
            }
        }
        return page;
    }

    @Override
    public void saveReport(EightDReport eightDReport) {
        //保存报告
        this.save(eightDReport);

        //保存文件
        if (CollectionUtils.isNotEmpty(eightDReport.getFileIds())) {
            FileBusiness fileBusiness = new FileBusiness();
            fileBusiness.setFileIds(eightDReport.getFileIds());
            fileBusiness.setBusinessId(eightDReport.getId());
            fileApi.updateBusinessById(fileBusiness);
        }
    }

    @Override
    public void updateReport(EightDReport eightDReport) {
        //修稿报告
        this.updateById(eightDReport);

        //修改文件
        if (CollectionUtils.isNotEmpty(eightDReport.getFileIds())) {
            FileBusiness fileBusiness = new FileBusiness();
            fileBusiness.setBusinessId(eightDReport.getId());
            fileBusiness.setFileIds(eightDReport.getFileIds());
            fileApi.updateBusinessById(fileBusiness);
        }
    }
}
